<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	template="../templates/ui.xhtml">
    
    <ui:define name="head">
        <style type="text/css">
            .ui-tree {
                width: 300px;
            }
        </style>
    </ui:define>

	<ui:define name="content">
		<h1 class="title ui-widget-header ui-corner-all">Tree - Checkbox Selection</h1>
		<div class="entry">
			<p>Node selection is a built-in feature and support three different modes, "single", "multiple" and "checkbox.</p>

			<h:form>

				<p:growl id="messages" showDetail="true" escape="false"/>

				<p:tree value="#{treeCheckboxBean.root2}" var="node"
						selectionMode="checkbox"
						selection="#{treeCheckboxBean.selectedNodes}">

					<p:treeNode>
						<h:outputText value="#{node}" />
					</p:treeNode>
				</p:tree>

				<p:commandButton value="Display Selected" update="messages" style="margin:20px 0 20px 0;"
								actionListener="#{treeCheckboxBean.displaySelectedMultiple}" id="btnDisplay"/>

			</h:form>

			<h3>Source</h3>
			<p:tabView>
				<p:tab title="treeSelectionCheckbox.xhtml">
					<pre name="code" class="xml">
&lt;h:form&gt;

    &lt;p:growl id="messages" showDetail="true" escape="false"/&gt;

    &lt;p:tree value="\#{treeCheckboxBean.root2}" var="node"
            selectionMode="checkbox"
            selection="\#{treeCheckboxBean.selectedNodes}"&gt;

        &lt;p:treeNode&gt;
            &lt;h:outputText value="\#{node}" /&gt;
        &lt;/p:treeNode&gt;
    &lt;/p:tree&gt;

    &lt;p:commandButton value="Display Selected" update="messages" style="margin:20px 0 20px 0;"
                    actionListener="\#{treeCheckboxBean.displaySelectedMultiple}" id="btnDisplay"/&gt;

&lt;/h:form&gt;
					</pre>
				</p:tab>

				<p:tab title="TreeCheckboxBean.java">
					<pre name="code" class="java">
package org.primefaces.examples.view;

import java.io.Serializable;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.model.TreeNode;

import org.primefaces.examples.domain.Document;
import org.primefaces.model.CheckboxTreeNode;

public class TreeCheckboxBean implements Serializable {
	
	private static final Logger logger = Logger.getLogger(TreeCheckboxBean.class.getName());

    private TreeNode root2;
    
    private TreeNode[] selectedNodes;
	
	public TreeCheckboxBean() {
        root2 = new CheckboxTreeNode("Root", null);
		TreeNode node0 = new CheckboxTreeNode("Node 0", root2);
		TreeNode node1 = new CheckboxTreeNode("Node 1", root2);
		TreeNode node2 = new CheckboxTreeNode("Node 2", root2);
		
		TreeNode node00 = new CheckboxTreeNode("Node 0.0", node0);
		TreeNode node01 = new CheckboxTreeNode("Node 0.1", node0);
		
		TreeNode node10 = new CheckboxTreeNode("Node 1.0", node1);
		TreeNode node11 = new CheckboxTreeNode("Node 1.1", node1);
		
		TreeNode node000 = new CheckboxTreeNode("Node 0.0.0", node00);
		TreeNode node001 = new CheckboxTreeNode("Node 0.0.1", node00);
		TreeNode node010 = new CheckboxTreeNode("Node 0.1.0", node01);
		
		TreeNode node100 = new CheckboxTreeNode("Node 1.0.0", node10);
	}
	    
    public TreeNode getRoot2() {
		return root2;
	}

	public void setRoot2(TreeNode root2) {
		this.root2 = root2;
	}
    
    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }

    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }
    
    public void displaySelectedMultiple() {
        if(selectedNodes != null &amp;&amp; selectedNodes.length &gt; 0) {
            StringBuilder builder = new StringBuilder();

            for(TreeNode node : selectedNodes) {
                builder.append(node.getData().toString());
                builder.append("&lt;br /&gt;");
            }

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());

            FacesContext.getCurrentInstance().addMessage(null, message);
        }
	}
}
					</pre>
				</p:tab>
			</p:tabView>

            </div>
	</ui:define>
</ui:composition>